package com.erp.framework.util;

import java.io.IOException;

import com.erp.framework.SimpleConstants;
import com.erp.framework.util.ftp.FTPClient;
import com.erp.framework.util.ftp.FTPException;

public class FtpUtil implements SimpleConstants{
    public FtpUtil() {
    }

    public void recursiveMkdir(FTPClient ftp, String dir) throws IOException, FTPException {
        if (ftp == null) {
            throw new FTPException("Null ftp client!");
        }

        dir = (dir == null) ? "" : dir.trim();
        if (dir.equals("")) {
            throw new FTPException("Empty directory name!");
        }

        String[] splitDir = CommonUtil.split(dir, "/");
        if (splitDir != null) {
            for (int i = 0; i < splitDir.length; i++) {
                splitDir[i] = (splitDir[i] == null) ? "" : splitDir[i].trim();
                if (splitDir[i].equals("")) {
                    throw new FTPException("Empty directory name!");
                }
            }
        }

        // perform the recursive way to mkdir
        try {
            String pwd = ftp.pwd();
            String tmpPwd = pwd;
            for (int i = 0; i < splitDir.length; i++) {
                String[] listDir = ftp.dir(tmpPwd);
                boolean found = false;
                if (listDir != null) {
                    for (int j = 0; j < listDir.length; j++) {
                        listDir[j] = (listDir[j] == null) ? "" : listDir[j].trim();
                        listDir[j] = CommonUtil.stringRpl(listDir[j], "\\", "/");
                        listDir[j] = CommonUtil.stringRpl(listDir[j], "//", "/");
                        if (listDir[j].equalsIgnoreCase(CommonUtil.stringRpl(tmpPwd + "/", "//", "/") + splitDir[i])) {
                            found = true;
                            break;
                        }
                    }
                }

                if (!found) {
                    ftp.mkdir(splitDir[i]);
                }
                ftp.chdir(splitDir[i]);
                tmpPwd = ftp.pwd();
            }

            // change back to the original directory
            ftp.chdir(pwd);
        } catch (IOException e) {
            throw new IOException(e.getMessage());
        } catch (FTPException e) {
            throw new FTPException(e.getMessage());
        }
    }

    public void recursiveRmdir(FTPClient ftp, String dir, int level) throws IOException, FTPException {
        if (ftp == null) {
            throw new FTPException("Null ftp client!");
        }

        dir = (dir == null) ? "" : dir.trim();
        if (dir.equals("")) {
            throw new FTPException("Empty directory name!");
        }

        try {
            if (!isDirExist(ftp, dir)) {
                return;
            }

            ftp.chdir(dir);
            String[] list = ftp.dir();
            if (list != null) {
                for (int i = 0; i < list.length; i++) {
                    list[i] = (list[i] == null) ? "" : list[i].trim();
                    list[i] = CommonUtil.stringRpl(list[i], "\\", "/");
                    list[i] = CommonUtil.stringRpl(list[i], "//", "/");
                    if (isDir(ftp, list[i])) {
                        recursiveRmdir(ftp, list[i], level + 1);
                    } else {
                        ftp.delete(list[i]);
                    }
                }
            }

            String[] tmpStr = CommonUtil.split(dir, "/");
            ftp.chdir("..");
            // in this stage all files should be removed then removed the empty
            // dir
            if (tmpStr != null && tmpStr.length > 0) {
                ftp.rmdir(tmpStr[tmpStr.length - 1]);
            }
        } catch (IOException e) {
            throw new IOException(e.getMessage());
        } catch (FTPException e) {
            throw new FTPException(e.getMessage());
        }
    }

    public boolean isDir(FTPClient ftp, String name) throws IOException, FTPException {
        boolean rslt = false;
        try {
            // a file can get the modification time
            ftp.modtime(name);
        } catch (IOException ex) {
            throw new IOException(ex.getMessage());
        } catch (FTPException ex) {
            try {
                // test if it is a directory
                String pwd = ftp.pwd();
                ftp.chdir(name);
                rslt = true;
                ftp.chdir(pwd);
            } catch (IOException e) {
                throw new IOException(e.getMessage());
            } catch (FTPException e) {
                throw new FTPException(e.getMessage());
            }
        }
        return rslt;
    }

    public boolean isDirExist(FTPClient ftp, String dir) throws IOException, FTPException {
        if (ftp == null) {
            throw new FTPException("Null ftp client!");
        }

        dir = (dir == null) ? "" : dir.trim();
        if (dir.equals("")) {
            throw new FTPException("empty directory name!");
        }

        if (dir.equals(".") || dir.equals("~")) {
            return true;
        }

        String[] splitDir = CommonUtil.split(dir, "/");

        if (splitDir != null) {
            for (int i = 0; i < splitDir.length; i++) {
                splitDir[i] = (splitDir[i] == null) ? "" : splitDir[i].trim();
                if (splitDir[i].equals("")) {
                    throw new FTPException("Empty directory name!");
                }
            }
        }

        try {
            String pwd = ftp.pwd();
            String tmpPwd = pwd;
            for (int i = 0; i < splitDir.length; i++) {
                String[] listDir = ftp.dir(tmpPwd);
                boolean found = false;
                if (listDir != null) {
                    for (int j = 0; j < listDir.length; j++) {
                        listDir[j] = (listDir[j] == null) ? "" : listDir[j].trim();
                        listDir[j] = CommonUtil.stringRpl(listDir[j], "\\", "/");
                        listDir[j] = CommonUtil.stringRpl(listDir[j], "//", "/");
                        if (listDir[j].equalsIgnoreCase(CommonUtil.stringRpl(tmpPwd + "/", "//", "/") + splitDir[i])) {
                            found = true;
                            break;
                        }
                    }
                }

                if (!found) {
                    ftp.chdir(pwd);
                    return false;
                }
                ftp.chdir(splitDir[i]);
                tmpPwd = ftp.pwd();
            }

            // change back to the original directory
            ftp.chdir(pwd);
        } catch (IOException e) {
            throw new IOException(e.getMessage());
        } catch (FTPException e) {
            throw new FTPException(e.getMessage());
        }

        return true;
    }

    public boolean isFileExist(FTPClient ftp, String dir, String filename) throws IOException, FTPException {
        if (ftp == null) {
            throw new FTPException("Null ftp client!");
        }

        dir = (dir == null) ? "" : dir.trim();
        if (dir.equals("")) {
            throw new FTPException("empty directory name!");
        }

        try {
            String pwd = ftp.pwd();

            if (!isDirExist(ftp, dir)) {
                return false;
            }

            ftp.chdir(dir);
            String[] listDir = ftp.dir();
            for (int j = 0; j < listDir.length; j++) {
                listDir[j] = (listDir[j] == null) ? "" : listDir[j].trim();
                listDir[j] = CommonUtil.stringRpl(listDir[j], "\\", "/");
                listDir[j] = CommonUtil.stringRpl(listDir[j], "//", "/");
                if (!isDir(ftp, listDir[j])) {
                    String[] temp = CommonUtil.split(listDir[j], "/");
                    if (temp != null) {
                        if (temp[temp.length - 1].equalsIgnoreCase(filename)) {
                            ftp.chdir(pwd);
                            return true;
                        }
                    }
                }
            }

            // change back to the original directory
            ftp.chdir(pwd);
        } catch (IOException e) {
            throw new IOException(e.getMessage());
        } catch (FTPException e) {
            throw new FTPException(e.getMessage());
        }

        return false;
    }

    // ERP: what does this fn do?
    /*
     * public String convertDir(String dir) { if (dir == null) { return ""; }
     * 
     * dir = CommonUtil.stringRpl(dir, " ", "_ErpSpace_"); dir =
     * CommonUtil.stringRpl(dir, "/", "_ErpSlash_");
     * 
     * return dir; }
     */
}